﻿#region Copyright
// Copyright (c) 2011-2012 RaisingForce Team (Alikin Sergey)
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
#endregion

using System;
using System.Collections.ObjectModel;
using System.Diagnostics.Contracts;
using System.Linq;
using RaisingForce.Raise.Hardware;
using RaisingForce.Raise.Hardware.Extensions;
using RaisingForce.Raise.Measures;

namespace RaisingForce.Raise.Presentation.ViewModel.Specialized
{
    public class SettingsViewModel :
        ViewModelBase
    {
        #region Constructors

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
        public SettingsViewModel(
            string title,
            IRobotHardwareOptions options,
            ISettingMeasures usingMeasures,
            string tachoMeasureText)
            : base()
        {
            if (options == null)
            {
                throw new ArgumentNullException("options");
            }
            if (usingMeasures == null)
            {
                throw new ArgumentNullException("usingMeasures");
            }
            Contract.EndContractBlock();
            this.DisplayName = title;
            this.options = new RobotHardwareOptions(options);
            this.usingMeasures = usingMeasures;
            this.tachoMeasureText = tachoMeasureText;
        }

        #endregion

        #region Private Declarations

        [ContractInvariantMethod]
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1822:MarkMembersAsStatic", Justification = "Required for code contracts.")]
        private void ObjectInvariant()
        {
            Contract.Invariant(this.options != null);
            Contract.Invariant(this.usingMeasures != null);
        }

        #endregion

        #region Get<Measure>Value Methods

        public double GetDistanceValue(
            Func<IRobotHardwareOptions, Distance> getDistanceFunc)
        {
            if (getDistanceFunc == null)
            {
                throw new ArgumentNullException("getDistanceFunc");
            }
            Contract.EndContractBlock();
            Distance distance = getDistanceFunc(this.options);
            return distance.Get(this.UsingMeasures.DistanceMeasure);
        }

        public double GetTimeValue(
            Func<IRobotHardwareOptions, Time> getTimeFunc)
        {
            if (getTimeFunc == null)
            {
                throw new ArgumentNullException("getTimeFunc");
            }
            Contract.EndContractBlock();
            Time time = getTimeFunc(this.options);
            return time.Get(this.UsingMeasures.TimeMeasure);
        }

        #endregion

        #region ViewModel Properties

        private ObservableCollection<SettingViewModel> inputControls;

        private const string propInputControls = "InputControls";

        public ObservableCollection<SettingViewModel> InputControls
        {
            get
            {
                Contract.Ensures(Contract.Result<ObservableCollection<SettingViewModel>>() != null);
                return this.GetNotNullProperty(
                    ref this.inputControls,
                    propInputControls);
            }
            set
            {
                this.SetProperty(
                    ref this.inputControls,
                    value,
                    propInputControls);
            }
        }

        #endregion

        #region Properties

        private RobotHardwareOptions options;

        public RobotHardwareOptions Options
        {
            get
            {
                Contract.Ensures(Contract.Result<RobotHardwareOptions>() != null);
                return this.options;
            }
            set
            {
                if (value == null)
                {
                    throw new ArgumentNullException("Options");
                }
                Contract.EndContractBlock();
                if (!object.ReferenceEquals(this.options, value))
                {
                    this.options = value;
                    foreach (SettingViewModel control in this.InputControls)
                    {
                        control.InvalidateValue();
                    }
                }
            }
        }

        private readonly ISettingMeasures usingMeasures;

        public ISettingMeasures UsingMeasures
        {
            get
            {
                Contract.Ensures(Contract.Result<ISettingMeasures>() != null);
                return this.usingMeasures;
            }
        }

        public bool IsValid
        {
            get
            {
                return this.InputControls == null
                    || this.InputControls.Count == 0 ?
                    true :
                    this.InputControls.All(vm => vm.IsValid);
            }
        }

        public string ComPortMeasureText
        {
            get { return null; }
        }

        private readonly string tachoMeasureText;

        public string TachoMeasureText
        {
            get { return this.tachoMeasureText; }
        }

        #endregion
    }
}
